# Prepare CA returns
# Note: clean and rearrange 2020 CA returns
# By Dom Valentino and Chris Kenny
# libs --------------------------------------------------------------------
library(tidyverse) # select(), filter(), mutate(), etc.
library(haven) # read_dta()

# data ---------------------------------------------------------------
returns <- read.csv("../data/returns_ca/returns_ca.csv")
cvap <- read_dta("../data/modified data/county_cvap.dta") # read cvap data

# returns ---------------------------------------------------------------
final_ca <- returns %>% transmute(year = 2020, state = "CA", treat = 1, county = county, fips = NA,
                                  dem_share_pres = dem_pres / (dem_pres + rep_pres), dem_share_gov = NA, dem_share_sen = NA,
                                  ballots_cast = ballots_cast) %>% 
  left_join(., cvap, by = c("year", "state", "county")) %>% 
  mutate(turnout_share = ballots_cast / cvap_approx)

# save
write_csv(final_ca, "../data/analysis/analysis_ca.csv")
